package com.newasia.server.common.checkHandler;

import com.newasia.server.common.exception.ValidateException;
import com.newasia.server.common.result.ManagementRetCode;
import com.newasia.server.common.tool.CheckObjectIsNullUtils;
import com.newasia.server.common.tool.SpringUtil;
import com.newasia.server.controller.form.UserForm;
import com.newasia.server.dal.entitys.User;
import com.newasia.server.dal.persistence.UserMapper;
import jodd.util.StringUtil;

/**
 * @author LiChong
 * @description 电话号码匹配
 * @date 2020/4/8
 */

public class MobileMatchHandler extends CheckHandler{

    UserMapper userMapper = SpringUtil.getBean(UserMapper.class);

    @Override
    public void doHandler(UserForm form) {

        User user = userMapper.selectByPrimaryKey(form.getUsername());
        if (CheckObjectIsNullUtils.checkObjIsNull(user)){
            throw new ValidateException(
                    ManagementRetCode.USERNAME_NOT_EXIST.getCode(),
                    ManagementRetCode.USERNAME_NOT_EXIST.getMessage());
        }
        if(!StringUtil.equals(user.getMobile(),form.getMobile())){
            throw new ValidateException(
                    ManagementRetCode.MOBILE_NOT_MATCH.getCode(),
                    ManagementRetCode.MOBILE_NOT_MATCH.getMessage());
        }
        if(null != next) {
            next.doHandler(form);
        }
    }
}
